package com.focusai.efairy.network.Image;

import android.content.Context;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemoryCache;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.target.ViewTarget;
import com.focusai.efairy.R;
import com.focusai.efairy.config.AppDirFileUtil;

@GlideModule
public class EFGlideModule extends AppGlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        ViewTarget.setTagId(R.id.image_view_tag);
        int memoryCacheSizeBySystem = (int) (Runtime.getRuntime().maxMemory() / 8);
        int memoryCacheSizeDefault = (int) (25 * 1024 * 1024);
        int memoryCacheSize = (int) (25 * 1024 * 1024);
        if (memoryCacheSizeBySystem > 0 && memoryCacheSizeBySystem < memoryCacheSizeDefault) {
            memoryCacheSize = memoryCacheSizeBySystem;
        }

        MemoryCache mMemCache = new LruResourceCache(memoryCacheSize);

        builder.setMemoryCache(mMemCache);
        int initSize;
        if (AppDirFileUtil.isCanUseSD()) {
            initSize = 500 * 1024 * 1024;//SDcard上
            builder.setDiskCache(new DiskLruCacheFactory(AppDirFileUtil.getGlideCacheDirPath(), initSize));
        } else {
            initSize = 100 * 1024 * 1024;//内存
            builder.setDiskCache(new InternalCacheDiskCacheFactory(context, initSize));
        }
    }

    @Override
    public boolean isManifestParsingEnabled() {
        //不使用清单配置的方式,减少初始化时间
        return false;
    }

    @Override
    public void registerComponents(Context context, Registry registry) {
        //配置glide网络加载框架
//        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
    }
}
